# -*- coding: UTF-8 -*-
"""
@Filename:exerc2.py
@author:Cora
@time:2025-03-28
"""

class StringProcessor:
    def __init__(self):
        pass

    @staticmethod
    def validate_input(input_str):
        """验证输入字符串长度是否在(0,255]之间"""
        return 0 < len(input_str) <= 255

    @staticmethod
    def filter_non_english(input_str):
        """过滤掉输入字符串中的所有非英文字符"""
        result = []
        for char in input_str:
            if 'a' <= char <= 'z' or 'A' <= char <= 'Z':
                result.append(char)
        return ''.join(result)

    @staticmethod
    def reverse_string(input_str):
        """反转字符串(不使用递归和内置reversed方法)"""
        reversed_chars = []
        length = len(input_str)
        for i in range(length - 1, -1, -1):
            reversed_chars.append(input_str[i])
        return ''.join(reversed_chars)

    @staticmethod
    def caesar_cipher(input_str, shift=3):
        """凯撒密码加密"""
        result = []
        for char in input_str:
            if 'a' <= char <= 'z':
                # 处理小写字母
                shifted = ord(char) + shift
                if shifted > ord('z'):
                    shifted -= 26
                result.append(chr(shifted))
            elif 'A' <= char <= 'Z':
                # 处理大写字母
                shifted = ord(char) + shift
                if shifted > ord('Z'):
                    shifted -= 26
                result.append(chr(shifted))
            else:
                # 理论上不应该有非英文字符，但保留原字符以防万一
                result.append(char)
        return ''.join(result)


def main():
    processor = StringProcessor()

    # 接收用户输入
    input_str = input("请输入一个字符串(1-255个字符): ")

    # 输入验证
    if not processor.validate_input(input_str):
        print("错误: 字符串长度必须在1-255个字符之间")
        return

    # 处理流程
    english_only = processor.filter_non_english(input_str)
    print(f"过滤非英文字符后: {english_only}")

    reversed_str = processor.reverse_string(english_only)
    print(f"反转字符串后: {reversed_str}")

    encrypted_str = processor.caesar_cipher(reversed_str, shift=3)
    print(f"凯撒加密后: {encrypted_str}")

if __name__ == "__main__":
    main()